package com.zaki.controller;

import com.zaki.aop.OperationLog;
import com.zaki.model.User;
import com.zaki.model.Result;
import com.zaki.service.UserService;
import com.zaki.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @OperationLog
    @PostMapping("/login")
    public Result login(String username,String password){
        //根据用户名和密码查询数据
        User user =userService.selectByUserNameAndPassword(username,password);
        //如果存在，则生成jwt令牌
        Map<String,Object> map =new HashMap<String, Object>();
        map.put("id",user.getId());
        map.put("name",user.getName());
        if(user !=null){
            String jwt = JWTUtils.generateJwt(map);
            return Result.success(jwt);
        }else{
            return Result.error("登录失败");
        }

    }
}
